<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" style="width:100%">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="hsettings(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                background
                :current-page="q.page"
                :page-size="q.pagesize"
                :page-sizes="[2,3, 5, 10]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'编辑角色':'新增角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogroles"
    >
      <el-form ref="rolesForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" style="width:80%" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" style="width:80%" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="hClear">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
      </el-dialog>
<el-dialog
  title="分配权限"
  width="50%"
  :visible.sync="dialogVisible"
  :close-on-click-modal="false"
  :close-on-press-escape="false">
  <AssignSettings @close="dialogVisible=false" :id='cutid'   v-if="dialogVisible"/>
</el-dialog>
  </div>
</template>

<script>
import { getroles, Delroles, Addroles, Editroles } from '@/api/roles'
import AssignSettings from './assignsettings.vue'
export default {
  components:{
    AssignSettings
  },
  data() {
    return {
      list: [],
      q: {
        page: 1,
        pagesize: 2
      },
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      total: null,
      dialogroles: false,
      isEdit: false,
      dialogVisible:false,
      cutid:''
    }
  },
  created() {
    this.loadgetroles()
  },
  methods: {
    async  loadgetroles() {
      // 表格渲染
      const res = await getroles(this.q)
      console.log('任务列表', res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.loadgetroles()
    },
    handleCurrentChange(page) {
      this.q.page = page
      this.loadgetroles()
    },
    hDel(id) {
      // 删除角色
      this.$confirm('确定要删除数据吗?', '提示', { type: 'warning' }).then(async() => {
        const res = await Delroles(id)
        console.log('删除数据', res)
        this.$message.success(res.message)
        if (this.list.length === 1 && this.q.page > 1) this.q.page--
        this.loadgetroles()
      })
        .catch(() => {})
    },
    // 新增角色
    hAdd() {
      this.isEdit = false
      this.dialogroles = true
    },
    // 编辑角色
    hEdit(row) {
      this.isEdit = true
      this.dialogroles = true
      this.form = { ...row }
    },
    // 表单校验判断是新增还是编辑
    async  hSubmit() {
      const valid = await this.$refs.rolesForm.validate().catch(e => e)
      if (!valid) return
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 新增请求
    async  doAdd() {
      try {
        const res = await Addroles(this.form)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        console.log('新增请求', res)
        this.dialogroles = false
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        // 跳转最后一页 向上取整
        this.q.page = Math.ceil(this.total / this.q.pagesize)
        this.loadgetroles()
      } catch (e) {
        console.log(e)
      }
    },
    // 编辑请求
    async  doEdit() {
      try {
        const res = await Editroles(this.form)
        if (res.code !== 1000) return this.$message.error(res.message)
        // if (res.code !== 1000) return this.$message.error(res.message)
        this.$message.success(res.message)
        console.log('编辑请求', res)
        this.dialogroles = false
        this.loadgetroles()
      } catch (e) {
        console.log(e)
      }
    },
    hClear() {
      // 关闭弹窗
      this.dialogroles = false
      // 清除表单校验
      this.$refs.rolesForm.resetFields()
      // 清空表单数据
      this.form = {
        name: '',
        description: ''
      }
    },
    hsettings(id){
      console.log('这是id',id);
      this.cutid=id
      this.dialogVisible=true
    }
  }
}
</script>
